package org.kabeja.parser.entities;

import org.kabeja.dxf.entity.DXFEntity;
import org.kabeja.dxf.entity.DXFMText;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 11:39
 */
public class DXFMTextHandler extends AbstractEntityHandler {
    public static final String ENTITY_NAME = "MTEXT";
    public static final int TEXT_VALUE_END = 1;
    public static final int TEXT_VALUE = 3;
    public static final int TEXT_HEIGHT = 40;
    public static final int TEXT_REF_WIDTH = 41;
    public static final int TEXT_REF_HEIGHT = 43;
    public static final int TEXT_DRAWING_DIRECTION_FLAG = 72;
    public static final int TEXT_ATTACHMENT_POINT = 71;
    public static final int TEXT_VALIGN = 73;
    public static final int TEXT_ALIGN_X = 11;
    public static final int TEXT_ALIGN_Y = 21;
    public static final int TEXT_ALIGN_Z = 31;
    public static final int TEXT_STYLE = 7;
    public static final int TEXT_OBLIQUEANGLE = 51;
    public static final int TEXT_ROTATION = 50;
    private DXFMText mtext;
    private StringBuffer buf = new StringBuffer();
    private int insert = 0;

    public DXFMTextHandler() {
    }

    @Override
    public void endDXFEntity() {
        this.mtext.setText(this.buf.toString());
        this.buf.delete(0, this.buf.length());
    }

    @Override
    public DXFEntity getDXFEntity() {
        return this.mtext;
    }

    @Override
    public String getDXFEntityName() {
        return "MTEXT";
    }

    @Override
    public boolean isFollowSequence() {
        return false;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case 1:
                this.buf.insert(this.insert, var2.getValue());
                break;
            case 3:
                String var3 = var2.getValue();
                this.buf.insert(this.insert, var3);
                this.insert += var3.length();
                break;
            case 7:
                this.mtext.setTextStyle(var2.getValue());
                break;
            case 10:
                this.mtext.setX(var2.getDoubleValue());
                break;
            case 11:
                this.mtext.setAlignX(var2.getDoubleValue());
                this.mtext.setRotation(0.0D);
                break;
            case 20:
                this.mtext.setY(var2.getDoubleValue());
                break;
            case 21:
                this.mtext.setAlignY(var2.getDoubleValue());
                this.mtext.setRotation(0.0D);
                break;
            case 30:
                this.mtext.setZ(var2.getDoubleValue());
                break;
            case 31:
                this.mtext.setAlignZ(var2.getDoubleValue());
                this.mtext.setRotation(0.0D);
                break;
            case 40:
                this.mtext.setHeight(var2.getDoubleValue());
                break;
            case 41:
                this.mtext.setReferenceWidth(var2.getDoubleValue());
                break;
            case 43:
                this.mtext.setReferenceHeight(var2.getDoubleValue());
                break;
            case 50:
                this.mtext.setRotation(var2.getDoubleValue());
                break;
            case 51:
                this.mtext.setObliqueAngle(var2.getDoubleValue());
                break;
            case 71:
                this.mtext.setAttachmentPoint(var2.getIntegerValue());
                break;
            case 72:
                switch(var2.getIntegerValue()) {
                    case 2:
                        this.mtext.setBackward(true);
                        return;
                    case 4:
                        this.mtext.setUpsideDown(true);
                        return;
                    default:
                        return;
                }
            default:
                super.parseCommonProperty(var1, var2, this.mtext);
        }

    }

    @Override
    public void parseObject(int var1, DXFValue var2, int type) {
        super.parseObjectProperty(var1, var2, type, this.mtext);
    }

    @Override
    public void startDXFEntity() {
        this.mtext = new DXFMText();
        this.insert = 0;
    }
}
